package br.edu.planodesaude.GUI.procedimento;

import java.text.ParseException;
import java.util.ArrayList;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

import br.edu.planodesaude.controle.Alteradora;
import br.edu.planodesaude.controle.Pesquisadora;
import br.edu.planodesaude.dominio.Especialidade;
import br.edu.planodesaude.dominio.Procedimento;
import br.edu.planodesaude.exceptions.SearchException;
import br.edu.planodesaude.exceptions.UpdateException;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author gustavo
 */
public class AtualizarProcedimento extends javax.swing.JInternalFrame {

    /**
     * Creates new form AtualizarProcedimento
     */
    public AtualizarProcedimento() {
    	setClosable(true);
    	setVisible(true);
    	setTitle("Alterar Procedimento");
        initComponents();
    }
    
    public void initJList(Especialidade esp){
		
		ArrayList<Especialidade> esps = new ArrayList<Especialidade>();
		
		try{
			esps = new Pesquisadora().getEspecialidades();
		}catch(SearchException sex){
			JOptionPane.showMessageDialog(null, sex.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
		}
		
		DefaultComboBoxModel model = new DefaultComboBoxModel(esps.toArray());
		
		model.setSelectedItem(esp);
		
		jComboBox1.setModel(model);
	}

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        alterarJButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        nomeJTextField = new javax.swing.JTextField();
        consultaJRadioButton = new javax.swing.JRadioButton();
        cirurgiaJRadioButton = new javax.swing.JRadioButton();
        buscarJButton = new javax.swing.JButton();
        precoJTextField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        alterarJButton.setText("Alterar");
        alterarJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                alterarJButtonActionPerformed(evt);
            }
        });

        jLabel1.setText("Nome");

        jLabel2.setText("Preço");

        jLabel4.setText("Tipo");

        buttonGroup1.add(consultaJRadioButton);
        consultaJRadioButton.setText("Consulta");

        buttonGroup1.add(cirurgiaJRadioButton);
        cirurgiaJRadioButton.setText("Cirurgia");

        buscarJButton.setText("Buscar");
        buscarJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buscarJButtonActionPerformed(evt);
            }
        });

        jLabel3.setText("Especialidade");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(alterarJButton, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(194, 194, 194))
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(71, 71, 71)
                        .addComponent(nomeJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(buscarJButton, javax.swing.GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(72, 72, 72)
                                .addComponent(precoJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel4))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(consultaJRadioButton)
                                    .addComponent(cirurgiaJRadioButton)
                                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(nomeJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(buscarJButton)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel2))
                    .addComponent(precoJTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(consultaJRadioButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(cirurgiaJRadioButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
                .addComponent(alterarJButton)
                .addContainerGap())
        );

        setBounds(434, 213, 500, 285);
    }// </editor-fold>                        
    
    Procedimento p = null;
    
    private void alterarJButtonActionPerformed(java.awt.event.ActionEvent evt) {
    	
    	if(nomeJTextField.getText().equals("")){
        	JOptionPane.showMessageDialog(null, "Informe o nome do procedimento", "Informação", JOptionPane.INFORMATION_MESSAGE);
    	}else if(precoJTextField.getText().equals("")){
    		JOptionPane.showMessageDialog(null, "Informe o preço do procedimento", "Informação", JOptionPane.INFORMATION_MESSAGE);
    	}else{
    		
    		try{
    			p.setNome(nomeJTextField.getText());
    			p.setPreco(Float.parseFloat(precoJTextField.getText()));
    			p.setEspecialidade((Especialidade)jComboBox1.getSelectedItem());
    			p.setTipo(consultaJRadioButton.isSelected() ? "consulta" : "cirurgia");
    			new Alteradora().alterarProcedimento(p);
    			JOptionPane.showMessageDialog(null, "Procedimento alterado com sucesso", "Informação", JOptionPane.INFORMATION_MESSAGE);
    			dispose();
    		}catch (UpdateException uex) {
    			JOptionPane.showMessageDialog(null, uex.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
    		}catch (NumberFormatException nex) {
				JOptionPane.showMessageDialog(null, "Informe um preço válido", "Informação", JOptionPane.INFORMATION_MESSAGE);
			}
    	}
    }                                              

    private void buscarJButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
        if(nomeJTextField.getText().equals("")){
        	JOptionPane.showMessageDialog(null, "Informe o nome do procedimento", "Informação", JOptionPane.INFORMATION_MESSAGE);
        }else{
        	
        	try{        		
        		p = new Pesquisadora().pesquisarProcedimento(nomeJTextField.getText());
        		precoJTextField.setText(Float.toString(p.getPreco()));
        		initJList(p.getEspecialidade());
        		if(p.getTipo().equals("cirurgia")) { 
        			cirurgiaJRadioButton.setSelected(true);
        		}else{ 
        			consultaJRadioButton.setSelected(true);
        		} 
        		cirurgiaJRadioButton.setEnabled(false);
        		consultaJRadioButton.setEnabled(false);
        	}catch(SearchException sex){
        		JOptionPane.showMessageDialog(null, sex.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
        	}
        }
    } 
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AtualizarProcedimento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AtualizarProcedimento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AtualizarProcedimento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AtualizarProcedimento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AtualizarProcedimento().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton alterarJButton;
    private javax.swing.JButton buscarJButton;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JRadioButton cirurgiaJRadioButton;
    private javax.swing.JRadioButton consultaJRadioButton;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField nomeJTextField;
    private javax.swing.JTextField precoJTextField;
    // End of variables declaration                   
}


